<template>
	<view>
		<view class="touxiang">
			<image :src="myInfo.portraitURL" @click="changephoto"></image>
		</view>
		<view class="userMsgbox">
			<view class="usernamebox">
				<view class="usernametext">用户名:</view>
				<view class="username">
					<input type="text" v-model="name" class="name"></input>
				</view>
			</view>
			<view class="sexbox">
				<view class="sextext">性别:</view>
				<view class="sex">
					<view class="man" @click="chooseman">
						<image src="../../static/unman.png" v-if="sex != 1" mode="aspectFit"></image>
						<image src="../../static/man.png" v-if="sex == 1" mode="aspectFit"></image>
						<text v-if="sex == 1" style="color: #1296db; font-weight: bold;">男</text>
						<text v-if="sex != 1">男</text>
					</view>
					<view class="woman" @click="chosewoman">
						<image src="../../static/unwoman.png" v-if="sex != 2" mode="aspectFit"></image>
						<image src="../../static/woman.png" v-if="sex == 2" mode="aspectFit"></image>
						<text v-if="sex == 2" style="color: #d4327a; font-weight: bold;">女</text>
						<text v-if="sex != 2">女</text>
					</view>
				</view>
			</view>
			<view class="telnumber">
				<view class="teltext">电话号码：</view>
				<view class="tel">
					<text class="number">{{myInfo.userId}}</text>
				</view>
			</view>
			<view class="telnumber">
				<view class="teltext">目标院校：</view>
				<view class="tel">
					<picker @change="choseuniversity" :value="index" :range="universityarray">
						<view class="number" style="color: #333333;">{{universityarray[index]?universityarray[index]:this.university}}</view>
					</picker>
				</view>
			</view>
			<view class="telnumber">
				<view class="teltext">目标专业：</view>
				<view class="tel">
					<picker @change="chosemajor" :value="number" :range="majorarray">
						<view class="number">{{majorarray[number]?majorarray[number]:this.major}}</view>
					</picker>
				</view>
			</view>
			<button class="button" @click="submit()">修改</button>
			<button class="buttonexit" @click="exit">退出</button>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			var userId = getApp().globalData.userId
			this.userId = getApp().globalData.userId
			console.log(this.userId)
			this.getMyInfo(this.userId)
		},
		data() {
			return {
				myInfo: {},
				userId: "",
				curr: 0,
				phonenumber: 12345678901,
				university: "",
				major: "",
				sex: 1,
				name: "",
				universityarray: ['清华大学', '北京大学', '中山大学','浙江工商大学','南京大学', '同济大学', '浙江大学', '上海交通大学'],
				index: null,
				number:null,
				majorarray: ['计算机技术','软件工程','人工智能','金融学', '经济学']
			}
		},
		methods: {
			submit(){
				var sexChinese = "男"
				if (this.sex == 1) {
					sexChinese = "男"
				} else {
					sexChinese = "女"
				}
				uni.showLoading({
					title:"加载中..."
				})
				uni.request({
					url: this.baseurl + "/changeMyMess?userId=" + this.userId,
					method: "PUT",
					header: {
						'content-type': "application/json"
					},
					data: {
						nickname: this.name,
						gender: sexChinese,
						school: this.universityarray[this.index],
						major: this.majorarray[this.number]
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading()
						//成功
						if (res.data.status === "success") {
							setTimeout(()=>{
								// 服务端响应的 message 提示
								uni.showToast({
									title: "修改成功",
									icon: "none",
									position: 'bottom',
								})
								//延时关闭  加载中的 loading框
								uni.hideLoading()
							},1000)
							setTimeout(() => {
								uni.reLaunch({
									url: "/pages/user/UserMsg"
								})
							},2000)
						} 
					}
				})
			},
			getMyInfo(userId) {
				uni.request({
				    url: this.baseurl + '/getMyMessage',
					data: {
						userId: userId
					},
				    success: (res) => {
						this.myInfo = res.data;
						this.name = res.data.nickname;
						console.log(res.data);
						if (this.myInfo.gender == "男") {
							this.sex = 1
						} else {
							this.sex = 2
						}
						for (var i = 0; i < this.universityarray.length; i++) {
							if (this.myInfo.school == this.universityarray[i]) {
								this.index = i
								console.log(this.index)
							}
						}
						for (var j = 0; j < this.majorarray.length; j++) {
							if (this.myInfo.major == this.majorarray[j]) {
								this.number = j
								console.log(this.number)
							}
						}
				    }
				});
			},
			changephoto(){
				uni.showToast({
					title:"修改头像"
				})
				// chooseImage() {
				// 	let that = this;
				// 	uni.chooseImage({
				// 		//该方法是调出选择图片的方法
				// 		count: 1, //数量限制
				// 		sizeType: ['original', 'compressed'], //可选 原图 或缩略图
				// 		success: function(res) {
				// 			console.log(res);
				// 			const tempFiles = res.tempFiles
				// 			let imgs = tempFiles.map((value, index)=> {
				// 				return {
				// 					name: "files", 
				// 					uri: value.path
				// 				}
				// 			})
				// 			console.log(imgs);
				// 			uni.uploadFile({
				// 				url: that.baseurl + "/uploadPicture",
				// 				files: imgs,
				// 				success: (uploadFileRes) => {
				// 					console.log(uploadFileRes);
				// 					var uploadImgObj = JSON.parse(uploadFileRes.data);
				// 					console.log(uploadImgObj);
				// 					console.log(uploadImgObj.picurls);
				// 					for (var i = 0; i < uploadImgObj.picurls.length; i++) {
				// 						that.imgUrl.push(uploadImgObj.picurls[i])
				// 						that.picId = uploadImgObj.picids[i]
				// 					}
				// 				}
				// 			})
				// 		}
				// 	});
			},
			chooseman() {
				this.sex = 1;
			},
			chosewoman() {
				this.sex = 2;
			},
			gotoUserMsg() {
				console.log("名字：" + this.name+ "性别：" +
					this.sex + "目标院校：" + this.university + "目标专业：" + this.major)
				uni.switchTab({
					url: "./UserMsg"
				})
			},
			choseuniversity: function(e) {
				//this.university = this.universityarray[e.target.value]
				this.index = e.target.value
				this.university = this.universityarray[e.target.value]
				console.log(this.university)
			},
			chosemajor: function(e) {
				//this.university = this.universityarray[e.target.value]
				this.number = e.target.value
				this.major = this.majorarray[e.target.value]
				console.log(this.major)
			},
			exit(){
				uni.showModal({
					content:'是否要退出登录',
					success:(res) => {
						if (res.confirm) {
							getApp().globalData.userId = "",
							uni.clearStorage();
							uni.reLaunch({
								url: "/pages/login/enter"
							})
							uni.showToast({
								title:"退出登录成功",
								icon:'none'
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.touxiang{
		margin-top: 100rpx;
		width: 100%;
		display: flex;
		justify-content: center;
		
		image{
			height: 200rpx;
			width: 200rpx;
			border-radius: 100rpx;
		}
	}
	
	.userMsgbox {
		display: flex;
		flex-direction: column;
		//width: 650rpx;
		//background-color: #007AFF;
		margin-top: 30rpx;
		//justify-content: flex-end;
		align-items: flex-end;
	
		.usernamebox {
			display: flex;
			flex-direction: row;
			align-items: center;
			height: 100rpx;
			//width: 600rpx;
			//background-color: #333333;
			justify-content: flex-end;
			margin-right: 30rpx;
	
			.usernametext {
				font-weight: bold;
				margin-right: 40rpx;
			}
	
			.username {
				display: flex;
				align-items: center;
				border-radius: 10rpx;
				height: 70rpx;
				width: 500rpx;
				background-color: #F2F2F2;
	
				.name {
					//background-color: #007AFF;
					padding-left: 10rpx;
					width: 500rpx;
				}
			}
		}
	
		.sexbox {
			display: flex;
			flex-direction: row;
			align-items: center;
			height: 100rpx;
			//width: 600rpx;
			//background-color: #333333;
			justify-content: flex-end;
			margin-top: 10rpx;
			margin-right: 30rpx;
	
			.sextext {
				font-weight: bold;
				margin-right: 40rpx;
			}
	
			.sex {
				border-radius: 10rpx;
				height: 70rpx;
				width: 500rpx;
				//background-color: #F2F2F2;
				display: flex;
				flex-direction: row;
				justify-content: space-around;
	
				.man {
					height: 70rpx;
					//background-color: #4CD964;
					display: flex;
					align-items: center;
					justify-content: center;
	
					image {
						height: 50rpx;
						width: 50rpx;
					}
	
					text {
						margin-left: 10rpx;
					}
	
				}
	
				.woman {
					height: 70rpx;
					display: flex;
					align-items: center;
					justify-content: center;
	
					image {
						height: 50rpx;
						width: 50rpx;
					}
	
					text {
						margin-left: 10rpx;
					}
				}
			}
		}
	
		.telnumber {
			display: flex;
			flex-direction: row;
			align-items: center;
			height: 100rpx;
			//width: 600rpx;
			//background-color: #333333;
			justify-content: flex-end;
			margin-right: 30rpx;
	
			.teltext {
				font-weight: bold;
				margin-right: 20rpx;
			}
	
			.tel {
				display: flex;
				align-items: center;
				border-radius: 10rpx;
				height: 70rpx;
				width: 500rpx;
				background-color: #F2F2F2;
	
				.number {
					//background-color: #007AFF;
					width: 500rpx;
					padding-left: 10rpx;
				}
			}
		}
	
		.subtitle {
			//background-color: #007AFF;
			margin-right: 40rpx;
		}
	}
	
	.button {
		margin-top: 300rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #FFFFFF;
		background-color: #007AFF;
		border-radius: 20rpx;
		height: 80rpx;
		width: 200rpx;
	}
	
	.buttonexit {
		margin-top: 50rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #FFFFFF;
		background-color: #ff0000;
		border-radius: 20rpx;
		height: 80rpx;
		width: 200rpx;
	}
</style>
